// src/utils/EnterpriseTypeDict.js
import enterpriseTypes from '@/constants/enterprise_types.json';

class EnterpriseTypeDict {
  constructor() {
    this.types = enterpriseTypes;
    this.codeToItem = new Map();
    this.parentToChildren = new Map();
    this.codeToFullName = new Map();

    this.buildIndex();
  }

  buildIndex() {
    this.codeToItem.clear();
    this.parentToChildren.clear();
    this.codeToFullName.clear();

    for (const primary of this.types) {
      // 索引一级分类
      this.codeToItem.set(primary.code, primary);
      this.codeToFullName.set(primary.code, primary.name);

      // 处理二级分类
      if (primary.children) {
        for (const child of primary.children) {
          // 索引二级分类
          this.codeToItem.set(child.code, child);
          const fullName = `${primary.name}-${child.name}`;
          this.codeToFullName.set(child.code, fullName);

          // 构建父级到子级的映射
          if (!this.parentToChildren.has(child.parentId)) {
            this.parentToChildren.set(child.parentId, []);
          }
          this.parentToChildren.get(child.parentId).push(child);
        }
      }

      // 也为一级分类构建子级映射
      if (!this.parentToChildren.has(primary.parentId)) {
        this.parentToChildren.set(primary.parentId, []);
      }
      this.parentToChildren.get(primary.parentId).push(primary);
    }
  }

  // ========== 公共查询方法 ==========

  /**
   * 获取完整的层级结构
   */
  getEnterpriseTypes() {
    return [...this.types];
  }

  /**
   * 获取所有一级分类
   */
  getPrimaryTypes() {
    return this.types.filter((item) => item.parentId === 0);
  }

  /**
   * 根据父级代码获取子级分类
   */
  getChildrenByParentCode(parentCode) {
    return this.parentToChildren.get(parentCode) || [];
  }

  /**
   * 根据代码获取企业类型项
   */
  getByCode(code) {
    return this.codeToItem.get(code);
  }

  /**
   * 根据代码获取完整名称
   */
  getFullNameByCode(code) {
    return this.codeToFullName.get(code) || String(code);
  }

  /**
   * 验证代码是否存在
   */
  isValidCode(code) {
    return this.codeToItem.has(code);
  }

  /**
   * 验证是否为有效的二级代码
   */
  isValidSecondaryCode(code) {
    const item = this.codeToItem.get(code);
    return item && item.parentId !== 0; // 二级分类的parentId不为0
  }

  /**
   * 获取所有有效的二级代码（用于表单选择）
   */
  getAllSecondaryCodes() {
    const codes = [];
    for (const primary of this.types) {
      if (primary.children) {
        for (const child of primary.children) {
          codes.push(child.code);
        }
      }
    }
    return codes;
  }

  /**
   * 根据二级代码获取对应的一级代码
   */
  getPrimaryCodeBySecondary(secondaryCode) {
    const item = this.codeToItem.get(secondaryCode);
    return item ? item.parentId : null;
  }

  /**
   * 获取扁平化列表（用于简单下拉选择）
   */
  getFlattenedList() {
    const result = [];
    for (const primary of this.types) {
      // 添加二级分类
      if (primary.children) {
        for (const child of primary.children) {
          result.push({
            code: child.code,
            name: child.name,
            fullName: `${primary.name}-${child.name}`,
            parentCode: primary.code,
            parentName: primary.name,
            level: 2,
            isLeaf: true,
          });
        }
      }
    }
    return result;
  }

  /**
   * 获取级联选择器数据格式
   */
  getCascadeOptions() {
    return this.types.map((primary) => ({
      value: primary.code,
      label: primary.name,
      children: primary.children
        ? primary.children.map((child) => ({
            value: child.code,
            label: child.name,
          }))
        : [],
    }));
  }
}

// 创建单例实例
const enterpriseTypeDict = new EnterpriseTypeDict();

export default enterpriseTypeDict;
